package com.blb.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community.Annotation.Log;
import com.blb.community.entity.SysDictData;
import com.blb.community.entity.SysDictType;
import com.blb.community.entity.vo.DictDataVo;
import com.blb.community.service.ISysDictDataService;
import com.blb.community.service.ISysDictTypeService;
import com.blb.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author 帅帅
 * @since 2022-07-02
 */
@Api(tags = "字典数据管理模块")
@RestController
@RequestMapping("/sys-dict-data")
public class SysDictDataController {

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @ApiOperation("根据字典数据类型分页查询字典数据")
    @GetMapping("/dictDatas/dictType")
    public ResponseResult<IPage<SysDictData>> getSysDictData(Long current,DictDataVo dictDataVo){
        IPage<SysDictData> allSysDictData = sysDictDataService.getAllSysDictData(new Page<>(dictDataVo.getCurrent(),5L),dictDataVo);
        return ResponseResult.ok(allSysDictData);
    }

    @ApiOperation("根据字典类型查询字典数据")
    @GetMapping("/dictDatas/dictData")
    public ResponseResult<IPage<SysDictData>> selectSysDictDataByType(Long dictId,DictDataVo dictDataVo){
        SysDictType dictType = sysDictTypeService.getById(dictId);
        dictDataVo.setDictType(dictType.getDictType());
        IPage<SysDictData> DictDataByType =  sysDictDataService.selectSysDictDataByType(new Page<>(dictDataVo.getCurrent(), 10L),dictDataVo);
        return ResponseResult.ok(DictDataByType);
    }
    @Log(module = "添加字典数据", operationType = 1, requestMethod = "POST")
    @ApiOperation("添加字典数据")
    @PostMapping("/dictData")
    public ResponseResult<String> addDictType(SysDictData dictData) {

        LocalDateTime now = LocalDateTime.now();
        dictData.setCreateTime(now);
        sysDictDataService.save(dictData);
        return ResponseResult.ok("添加成功");
    }

    @Log(module = "修改字典类型", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改字典类型")
    @PutMapping("/dictData")
    public ResponseResult<String> updateDictType(@RequestBody SysDictData sysDictData) {

        LocalDateTime now = LocalDateTime.now();
        sysDictData.setUpdateTime(now);
        sysDictDataService.updateById(sysDictData);
        return ResponseResult.ok("修改成功");
    }


    @Log(module = "单个删除字典数据", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("单个删除字典数据")
    @DeleteMapping("/dictData/{dictCode}")
    public ResponseResult<String> deleteDictType(@PathVariable Long dictCode) {

        sysDictDataService.logicDeleteDictData(dictCode);
        return ResponseResult.ok("删除成功");
    }
    @Log(module = "批量删除字典数据信息", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("批量删除字典数据信息")
    @DeleteMapping("/dictDatas/{arr}")
    public ResponseResult<String> deleteDictDatas(@PathVariable ArrayList<Long> arr) {
        sysDictDataService.removeByIds(arr);
        return ResponseResult.ok("批量删除成功");
    }

    @ApiOperation("查询所有dictType对应的dictData")
    @GetMapping("/SysDictData/dictType")
    public ResponseResult<List<SysDictData>> selectAll(String dictType){
        List<SysDictData> list = sysDictDataService.list(new QueryWrapper<SysDictData>().lambda().eq(SysDictData::getDictType, dictType));
        return ResponseResult.ok(list);
    }
}
